package com.snopy.controller;

import com.snopy.common.JSONResult;
import com.snopy.pojo.Users;
import com.snopy.pojo.bo.UserBO;
import com.snopy.service.UserService;
import com.snopy.utils.CookieUtils;
import com.snopy.utils.JsonUtils;
import com.snopy.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * TODO
 *
 * @author snoopy
 * @date 2020/9/19 0019 15:10
 * @email 77912204@qq.com
 * @remark
 */
@Api(value = "注册登录",tags = {"用于注册登录的相关接口"})
@RestController
@RequestMapping("passport")
public class PassportController {
    final static Logger logger = LoggerFactory.getLogger(PassportController.class);
    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户名是否存在",notes = "用户名是否存在",httpMethod = "GET")
    @RequestMapping("/usernameIsExist")
    public JSONResult usernameIsExist(@RequestParam String username){
        if (StringUtils.isEmpty(username)){
            return JSONResult.errorMsg("用戶名不可为空！");
        }
        boolean b = userService.queryUsernameIsExist(username);
        if (b){
            return JSONResult.errorMsg("用戶名重复！");
        }
        return JSONResult.ok();
    }
    @ApiOperation(value = "用户注册", notes = "用户注册", httpMethod = "POST")
    @PostMapping("/register")
    public JSONResult register(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response){

        String username = userBO.getUsername();
        String password = userBO.getPassword();
        String confirmPwd = userBO.getConfirmPassword();
        //0.判断用户名和密码必须不为空
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            return JSONResult.errorMsg("用户名或密码不可为空！");
        }

        //1.查询用户名是否存在
        boolean b = userService.queryUsernameIsExist(username);
        if (b){
            return JSONResult.errorMsg("该用户名已存在！");
        }
        //2.密码长度不能少于6位
        if(password.length() <6){
            return JSONResult.errorMsg("密码长度不能少于6");
        }
        //3.判断两次密码是否一致
        if (!password.equals(confirmPwd)) {
            return JSONResult.errorMsg("两次密码输入不一致");
        }
        //4.实现注册
        Users user = userService.createUser(userBO);
        user = setPropertyNUll(user);
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(user),true);
        logger.info("注册成功");
        return JSONResult.ok(user);
    }
    @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "POST")
    @PostMapping("/login")
    public JSONResult login(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response) throws Exception {

        String username = userBO.getUsername();
        String password = userBO.getPassword();
        //0.判断用户名和密码必须不为空
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            return JSONResult.errorMsg("用户名或密码不可为空！");
        }


        //1.登录
        Users user = userService.queryUserForLogin(username, MD5Util.getMD5Str(password));
        if (user==null){
            return JSONResult.errorMsg("用户名或密码错误！");
        }
        user = setPropertyNUll(user);
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(user),true);
        return JSONResult.ok(user);
    }
    @ApiOperation(value = "用户退出登录", notes = "用户退出登录", httpMethod = "POST")
    @PostMapping("/logout")
    public JSONResult logout(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response) throws Exception {

        //清楚cookie信息
        CookieUtils.deleteCookie(request,response,"user");

        // TODO 用户退出登录，需要清空购物车
        // TODO 分布式会话中需要清除用户数据

        return JSONResult.ok();
    }
    private Users setPropertyNUll(Users users){
        users.setPassword(null);
        users.setMobile(null);
        users.setEmail(null);
        users.setCreatedTime(null);
        users.setUpdatedTime(null);
        users.setBirthday(null);
        return users;
    }
}
